using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(CharacterSystem))]
public class CharacterItemDroper : NetworkBehaviour
{
	public bool DropSplit = true;

	public GameObject Backpack;

	private CharacterSystem character;

	private static int kCmdCmdDropItem;

	private void Start()
	{
		character = GetComponent<CharacterSystem>();
	}

	[Command(channel = 0)]
	private void CmdDropItem(string itemdata)
	{
		if (DropSplit)
		{
			ItemManager itemManager = (ItemManager)Object.FindObjectOfType(typeof(ItemManager));
			string[] array = itemdata.Split("|"[0]);
			if (array.Length < 4)
			{
				return;
			}
			string[] array2 = array[0].Split(","[0]);
			string[] array3 = array[1].Split(","[0]);
			string[] array4 = array[2].Split(","[0]);
			string[] array5 = array[3].Split(","[0]);
			for (int i = 0; i < array2.Length; i++)
			{
				if (array2[i] != string.Empty)
				{
					ItemCollector itemCollector = new ItemCollector();
					itemCollector.Item = itemManager.GetItemDataByID(array2[i]);
					if (itemCollector.Item != null)
					{
						int.TryParse(array3[i], out itemCollector.Num);
						int.TryParse(array4[i], out itemCollector.NumTag);
						int.TryParse(array5[i], out itemCollector.Shortcut);
						itemCollector.Active = true;
						UnitZ.gameNetwork.RequestSpawnItem(itemCollector.Item.gameObject, itemCollector.NumTag, itemCollector.Num, base.transform.position, Quaternion.identity);
					}
				}
			}
		}
		else if ((bool)Backpack)
		{
			UnitZ.gameNetwork.RequestSpawnBackpack(Backpack.gameObject, itemdata, base.transform.position, Quaternion.identity);
		}
	}

	public void DropItem()
	{
		if (base.isLocalPlayer && character != null && character.inventory != null)
		{
			CallCmdDropItem(character.inventory.GetItemDataText());
		}
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeCmdCmdDropItem(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdDropItem called on client.");
		}
		else
		{
			((CharacterItemDroper)obj).CmdDropItem(reader.ReadString());
		}
	}

	public void CallCmdDropItem(string itemdata)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdDropItem called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdDropItem(itemdata);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdDropItem);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(itemdata);
		SendCommandInternal(networkWriter, 0, "CmdDropItem");
	}

	static CharacterItemDroper()
	{
		kCmdCmdDropItem = -1291958086;
		NetworkBehaviour.RegisterCommandDelegate(typeof(CharacterItemDroper), kCmdCmdDropItem, InvokeCmdCmdDropItem);
		NetworkCRC.RegisterBehaviour("CharacterItemDroper", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool result = default(bool);
		return result;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
	}
}
